package com.gscitysfy.cus.drain.modules.command.convert;

import com.gscitysfy.cus.drain.modules.command.dto.InstructTeamDto;
import com.gscitysfy.cus.drain.modules.command.dto.ResourceDispatchDto;
import com.gscitysfy.cus.drain.modules.command.entity.InstructTeam;
import com.gscitysfy.cus.drain.modules.command.service.IInstructTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author jiang_wen
 * @Date 2022/10/24 19:38
 * @Description TODO
 */
@Component
public class InstructTeamConvert {

    @Autowired
    @Lazy
    private IInstructTeamService iInstructTeamService;

    public InstructTeam toEntity(InstructTeam instructTeam, InstructTeamDto instructTeamDto) {
        instructTeam.setDangerSituationId(instructTeamDto.getDangerSituationId());
        instructTeam.setTeamId(instructTeamDto.getTeamId());
        instructTeam.setMessageTemplate(instructTeamDto.getMessageTemplate());
        instructTeam.setStatus(instructTeamDto.getStatus());
        return instructTeam;
    }

    public List<InstructTeam> toEntityList(ResourceDispatchDto resourceDispatchDto) {
        List<InstructTeam> instructTeamList = new ArrayList<>();
        List<InstructTeamDto> instructTeamDtoList = resourceDispatchDto.getInstructTeamDtoList();

        Set<Long> selectedTeamId = iInstructTeamService.fetchByDangerSituation(resourceDispatchDto.getDangerSituationId())
                .stream()
                .map(InstructTeam::getTeamId)
                .collect(Collectors.toSet());
        List<InstructTeamDto> filterInstructTeamDtoList = instructTeamDtoList
                .stream()
                .filter(t -> !selectedTeamId.contains(t.getTeamId()))
                .collect(Collectors.toList());

        InstructTeam instructTeam;
        for (InstructTeamDto instructTeamDto : filterInstructTeamDtoList) {
            instructTeam = new InstructTeam();
            instructTeamDto.setStatus("PENDING");
            instructTeamList.add(toEntity(instructTeam, instructTeamDto));
        }
        return instructTeamList;
    }
}
